import { createRouter, createWebHistory } from "vue-router";
import Home from "../views/Home.vue";
import Login from "../views/Login.vue";
import Adminlogin from "../views/Adminlogin.vue";
import Admin from "../views/Admin.vue";

import Category from "../views/main/Category.vue";
import HomeBanner from "../views/main/HomeBanner.vue";

import Cart from "../views/main/Cart.vue";
import Address from "../views/main/Address.vue";
import Personal from "../views/main/Personal.vue";
import UserInfo from "../views/admin/UserInfo.vue";
import BookInfo from "../views/admin/BookInfo.vue";
import AdminLayout from "../views/admin/AdminLayout.vue";
import Order from "../views/main/Order.vue";
import OrderForm from "../views/main/OrderForm.vue";
import Search from "../views/main/Search.vue";
import OrderInfo from "../views/admin/OrderInfo.vue";
import { ElMessageBox } from 'element-plus';
import { definedUser } from "@/store/store";
import API from "@/api/API";
const routes = [
  {
    path: "/",
    redirect:"/home"
    
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/home",
    name: "home",
    component: Home,
    meta: {
      isLogin: false,
    },
    children: [
      {
        //轮播图组件
        path: "",
        name: "HomeBanner",
        component: HomeBanner,
        meta: {
          isLogin: false,
        },
      },
      {
        // path: "Category",
        path: "Category/:categoryId",
        name: "Category",
        component: Category,
        meta: {
          isLogin: false,
        },
      },

      {
        //购物车
        path: "Cart",
        name: "Cart",
        component: Cart,
        meta: {
          isLogin: false,
        },
      },
      //收货地址管理
      {
        path: "Address",
        name: "Address",
        component: Address,
        meta: {
          isLogin: false,
        },
      },
      {
        //个人中心
        path: "Personal",
        name: "Personal",
        component: Personal,
         meta: {
          isLogin: false,
        },
      },
      {
        //订单详情
        path:"Order",
        name:"Order",
        component:Order,
         meta: {
          isLogin: false,
        },
      },
      {
        //填写订单
        path:"OrderForm",
          name:"OrderForm",
            component:OrderForm,
             meta: {
          isLogin: false,
        },
      },
      {
        path:"Search",
        name:"Search",
        component:Search,
        meta:{
          isLogin:false,
        }
      },
    ],
  },
  {
    path: "/Adminlogin",
    name: "Adminlogin",
    component: Adminlogin,
    meta: {
      isLogin: true,
    },
  },
  {
    path: "/Admin",
    name: "Admin",
    component: Admin,
    children:[
      {
        path:"",
        name:"AdminLayout",
        component:AdminLayout,

      },
      {
        path:"/UserInfo",
        name:"UserInfo",
        component:UserInfo,
      },
      {
        path:"/BookInfo",
        name:"BookInfo",
        component:BookInfo,
      },
      {
        path:"/OrderInfo",
        name:"OrderInfo",
        component:OrderInfo,
      }
    ]
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});



 
let intervalId;

router.beforeEach((to, from, next) => {
  // 在每次路由跳转之前执行的逻辑
  clearInterval(intervalId); // 清除之前可能存在的定时器
  if (to.path !== '/login') {
    const token = localStorage.getItem('token'); // 从本地存储获取 token
    //如果 token 不存在且当前页面不是登录页，则执行以下逻辑
        if(token == null){
            ElMessageBox.confirm('登录已过期，请重新登录。点击确认跳转到登录页面。', '提示', {
            confirmButtonText: '确认',
            type: 'warning',
          }).then(() => {
            // 用户点击确认后跳转到登录页面
            console.log("then")
            router.push({ path: "/login", name: "Login" });
          }, () => {
            // 用户点击取消或其他操作时的处理
          });
        }
  }
  // 继续路由导航
  next();
});

// //使用 beforeEach 守卫来检查 token 是否存在
// router.beforeEach((to, from, next) => {
//   const token = localStorage.getItem('token'); // 从本地存储获取 token
  
//     if (token == null) {
//       // 如果没有 token，则重定向到登录页面
//       next('/login');
//     } else {
//       // 如果有 token，则继续导航
//       next();
//     }
//   }
// );

export default router;
